/**
 *
 */
package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @author Alon Segal
 *
 */
public class ForumClientListener implements Runnable {

	private ConnectorWithGui connector;
	private Socket socket;
	private boolean isWorking;

	/**
	 *
	 */
	public ForumClientListener(Socket s) {
		this.connector = new ConnectorWithGui();
		this.socket = s;
		this.isWorking = true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			while (this.isWorking) {
				String line = "";
				char ch = '0';
				while (this.isWorking && ch != '\0') {
					ch = (char)in.read();
					line += ch;
				}
				if(isWorking)
					connector.onData(line);
			}
		} catch (IOException e) {
			System.out.println("IO problem occoured: "+e.getMessage());
		} catch (Exception e) {
			System.out.println("Problem occoured: "+e.getMessage());
		}
	}

	public void close() {
		this.isWorking = false;
	}

	public ConnectorWithGui getConnector() {
		return connector;
	}
	
	

}
